package com.eas.task.web.module.screen.statistic;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.TurbineRunData;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.eas.biz.service.exam.domain.Exam;
import com.eas.biz.service.exam.interfaces.ExamService;
import com.eas.task.common.job.TaskJobWrapper;
import com.eas.task.examStatistic.BaseStatisticJob;
import com.eas.task.examStatistic.ClassStatisticJob;
import com.eas.task.examStatistic.SchoolStatisticJob;
import com.eas.task.examStatistic.StudentStatisticJob;

public class Index implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Resource(name = "examService")
    private ExamService examService;

    private ExecutorService executor = Executors.newFixedThreadPool(1);

    private Map<String, TaskJobWrapper> jobMap = new ConcurrentHashMap<String, TaskJobWrapper>();

    public void execute(@Param(name = "type") String type, @Param(name = "examIds") String examIds,
            @Param(name = "uuid") String uuid, Navigator nav, TurbineRunData rundata,
            Context context) {
        if ("submit".equals(type) && StringUtils.isNotBlank(examIds)) {
            submitJobs(examIds);
            nav.redirectTo("statisticLink").withTarget("index");
            return;
        } else if ("remove".equals(type) && uuid != null) {
            removeJob(uuid);
            nav.redirectTo("statisticLink").withTarget("index");
            return;
        }

        context.put("jobList", jobMap.values());
    }

    private void removeJob(String uuid) {
        synchronized (jobMap) {
            jobMap.remove(uuid);
        }
    }

    private void submitJobs(String examIds) {
        String[] values = examIds.split(",");
        for (String value : values) {
            if (!StringUtils.isNumeric(value)) {
                continue;
            }
            Exam exam = examService.getSimpleExam(Long.valueOf(value));
            if (exam != null) {
                BaseStatisticJob job = (BaseStatisticJob) applicationContext
                        .getBean("examStatisticJob");
                job.setStatisticExamIds(String.valueOf(examIds));
                job.setStatisticTarget(SchoolStatisticJob.JOB_TARGET + ","
                        + ClassStatisticJob.JOB_TARGET + "," + StudentStatisticJob.JOB_TARGET);
                TaskJobWrapper wrapper = new TaskJobWrapper(job);
                wrapper.setInfo(exam.getTitle());
                wrapper.setId(String.valueOf(exam.getId()));
                synchronized (jobMap) {
                    jobMap.put(wrapper.getUuid(), wrapper);
                    executor.submit(wrapper);
                }
            }
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
